import java.io.*;
import java.security.*;
        
public class PasswordGenerater {

    public static void main(String args[]) {
        
	if (args.length != 4) {
	    System.out.println("===========================================================");
	    System.err.println("usage: [passwd file name] [user_name] [password] [password]");
	    System.out.println("======================================================");
	    System.exit(0);
	}
	
	String file_name = args[0];
	String user_name = args[1];
	String password1 = args[2];
	String password2 = args[3];

	if (!password1.equals(password2)) {
	    System.out.println("======================================================");
	    System.out.println("password1 != password2");
	    System.out.println("======================================================");
	    System.exit(0);
	}

	Runtime rt = Runtime.getRuntime();
	Process p = null;
	String cmd = "C:/myServers/ApacheGroup/Apache2/bin/htpasswd -b D:/myServers/ApacheGroup/Apache2/passwd/."+
	             file_name +" "+ user_name+" "+password1;
	InputStream inp  = null;
	InputStream inpE = null;

	try {
	    p = rt.exec(cmd);
	    p.waitFor();

	    inp = p.getInputStream();
	    inpE= p.getErrorStream();
	}
	catch (Exception e) {
	    System.out.println("Error!");
	}

	try {
	    int count  = inp.available();
	    int countE = inpE.available();

	    if (countE>0) {

		byte[] bufferE = new byte[countE];

		inpE.read(bufferE);

		String stderr = new String(bufferE);

		System.out.println("Standard Error:"+ stderr);
	    }
	    else {
		System.out.println("No output to standard error!");
	    }

	    if (count>0) {

		byte[] buffer = new byte[count];

		inp.read(buffer);

		String stdout = new String(buffer);
		System.out.println("Standard Output:"+ stdout);
	    }
	    else {
		System.out.println("No output to standard output!");
	    }

	}
	catch (Exception e) {
	    System.out.println("Error in output!");
	}
    }
}

